package com.a2j.app.car.swagger;

import com.a2j.beans.base.code.ResponseCode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: bzb
 * Data: 2022/1/20 17:56
 * Desc: Swagger Api 启动配置项
 * Docs: http://localhost:1234/doc.html
 */
@Configuration
@EnableSwagger2WebMvc // 开启Swagger web服务
public class AppCarApiConfig {

    @Bean
    public Docket appCarSwagger() {

        // 全局响应码统一处理
        List<ResponseMessage> responseMessages = new ArrayList<>();
        for (ResponseCode value : ResponseCode.values()) {
            // 大于1000的不显示
            if (value.getCode() > 1000) continue;

            responseMessages.add(
                    new ResponseMessageBuilder()
                            .code(value.getCode())
                            .message(value.getMessage())
                            .build()
            );
        }

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) // Api配置
                .groupName("app汽车模块") // 分组名称
                .useDefaultResponseMessages(false) // 不使用默认的响应码描述
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages)
                .select() // 构建选择器builder配置
                .apis(RequestHandlerSelectors.basePackage("com.a2j.app.car.controller")) // 扫描接口的包路径
                .paths(PathSelectors.any()) // 扫描路径
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("A2J-app汽车接口文档")
                .description("接口描述、参数描述、字段描述、在线调试")
                .termsOfServiceUrl("http://localhost:1234/app-car/doc.html")
                .version("1.0.0")
                .contact(new Contact("bzb", "https://gitee.com/su22/springboot", "bzb2419@163.com"))
                .build();
    }

}
